package com.naoh.retry.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.remoting.RemoteAccessException;
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Recover;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;

import java.time.LocalTime;

/**
 * TODO
 *
 * @author yushaojian
 * @date 2018/8/16 16:36
 */
@Service
public class RemoteService {
    private final static Logger logger = LoggerFactory.getLogger(RemoteService.class);

    @Retryable(value = { RemoteAccessException.class }, maxAttempts = 3, backoff = @Backoff(delay = 5000L, multiplier = 1))
    public String call(Integer i) {
        logger.info(LocalTime.now()+" do something...");
        if(i==2){
            throw new RemoteAccessException("RPC调用异常");
        }else{
            logger.info(LocalTime.now()+" todo...");
        }
        return "retryable";
    }

    @Recover
    public String recover(RemoteAccessException e) {
        // 此处可以抛出异常，上层捕获处理业务
        logger.info(e.getMessage());
        return "recover";
    }
}
